package com.ds.sign.config;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;

/**
 * 运单加签
 * @author 华为荣耀
 *
 */
@Component
public class CEB511SignSocketServer 
{
	private static Socket instance;
	
	public static void closeSocket() 
	{
		if (null != instance) 
		{
			try 
			{
				instance.close();
			} 
			catch (IOException e) 
			{
				
			}
		}
		instance = null;
	}
	
	public static Socket getSocket() 
	{
		return instance;
	}
	
	/**
	 * 启动订单加签服务-服务器端
	 */
	@PostConstruct
	public static void startSocket() 
	{
		new Thread(new Runnable() 
		{
			@Override
			public void run() 
			{
				// 1.创建Socket服务端，监听指定端口
				try 
				{
					ServerSocket serverSocket = new ServerSocket(6667);
					while (true) 
					{
						instance = serverSocket.accept();
						System.out.println("运单加签服务Socket连接已建立");
					}
				}
				catch (IOException e) 
				{
					System.out.println("运单加签服务Socket启动失败。" + e.toString());
				}
			}
		}).start();
	}
}
